package com.school.repair.controller;

import com.school.repair.entity.*;
import com.school.repair.service.*;
import com.school.repair.util.AjaxResult;
import com.school.repair.util.Page;
import com.school.repair.util.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class AdminController {


    @Autowired
    AdminService as;
    @Autowired
    RepairunitService rs;
    @Autowired
    EquipmentService es;
    @Autowired
    BaoXiuDanService bxds;
    @Autowired
    BaoXiuManService bxms;
    @Autowired
    RepairManService rms;
    @Autowired
    TodoService ts;

    @RequestMapping(value = "Adminlogin", produces = "application/json;charset=utf-8")
    public AjaxResult login(@RequestParam("username")String username,@RequestParam("password")String password, HttpSession session) {
        /*String username = admin.getUsername();
        String password = admin.getPassword();*/
        Admin user = as.login(username,password);
        if (user != null) {
            session.setAttribute("login", "666");
            return AjaxResult.success(user);
        }
        return AjaxResult.error("用户名或者密码不正确！");
    }
    @RequestMapping(value = "adminAdd", produces = "application/json;charset=utf-8")
    protected AjaxResult adminAdd(Admin admin) {
        int add = as.adminAdd(admin);
        if (add > 0)
            return AjaxResult.success("添加成功");
        return AjaxResult.error("添加失败");
    }

   @RequestMapping(value = "adminVerification", produces = "application/json;charset=utf-8")
    public AjaxResult adminVerification(@RequestParam(name = "id") long id, @RequestParam(name = "password") String password) {
        Admin admin = as.adminVerification(id, password);
        if (admin != null) {
            return AjaxResult.success("初始密码正确");
        } else {
            return AjaxResult.error("初始密码错误");
        }
    }

    @RequestMapping(value = "adminUsernameVal", produces = "application/json;charset=utf-8")
    protected AjaxResult adminUsernameVal(@RequestParam("username")String username){
        int checking = as.checking(username);
        if (checking > 0)
            return AjaxResult.error("用户名重复，请重新输入");
        return AjaxResult.success("用户名可用，请继续输入");
    }

    @RequestMapping(value = "updatePass", produces = "application/json;charset=utf-8")
    public AjaxResult updatePass(@RequestParam(name = "id") long id, @RequestParam(name = "password") String password) {
        int update2 = as.updatePass(id, password);
        if (update2 > 0)
            return AjaxResult.success("密码修改成功");
        return AjaxResult.error("密码修改失败");
    }

    @RequestMapping(value = "findById", produces = "application/json;charset=utf-8")
    public AjaxResult findById(@RequestParam(name = "id") long id) {
        Admin admin = as.findById(id);
        if (admin != null) {
            return AjaxResult.success(admin);
        } else {
            return AjaxResult.error("获取管理员失败");
        }
    }

    @RequestMapping(value = "updateData", produces = "application/json;charset=utf-8")
    public AjaxResult updateData(@RequestParam(name = "id") long id, @RequestParam(name = "username") String name, @RequestParam(name = "tele") String tele) {
        int update = as.updateData(id, name, tele);
        if (update > 0)
            return AjaxResult.success("更新成功");
        return AjaxResult.error("更新失败");
    }

    @RequestMapping(value = "adminList", produces = "application/json;charset=utf-8")
    public Page<Admin> adminList(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int pageSize, @RequestParam(name = "username", required = false) String name, @RequestParam(name = "tele", required = false) String tele) {
        return as.adminList(pageNo, pageSize, name, tele);
    }

    @RequestMapping(value = "delAdmin", produces = "application/json;charset=utf-8")
    public AjaxResult delAdmin(@RequestParam("id")long id){
        int r=0;
        try{
            r= as.delAdmin(id);
        }catch (Exception e){}
        if (r>0)
            return AjaxResult.success("删除成功");
        return AjaxResult.error("删除失败");
    }

    @RequestMapping(value = "repairunitPage", produces = "application/json;charset=utf-8")
    public Page<Repairunit> repairunitPage(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int pageSize, @RequestParam(name = "uname", required = false) String uname, @RequestParam(name = "linktel", required = false) String linktel, @RequestParam(name = "linkman", required = false) String linkman) {
        return rs.repairList(pageNo, pageSize, uname, linktel, linkman);
    }

    @RequestMapping(value = "repairunitAdd", produces = "application/json;charset=utf-8")
    public AjaxResult repairunitAdd(Repairunit unit) {
        int r = 0;
        try {
            r = rs.repairunitAdd(unit);
        } catch (Exception e) {
        }
        if (r == 0) {
            return AjaxResult.error("添加单位失败");
        }
        return AjaxResult.success("添加单位成功");
    }

    @RequestMapping(value = "repairunitFind", produces = "application/json;charset=utf-8")
    public AjaxResult repairunitFind(@RequestParam("id") long id) {
        Repairunit unit = rs.repairunitFind(id);
        if (unit != null) {
            return AjaxResult.success(unit);
        }
        return AjaxResult.error("此单位不存在");
    }

    @RequestMapping(value = "repairunitUpdate", produces = "application/json;charset=utf-8")
    protected AjaxResult repairunitUpdate(@RequestParam("id") long id, @RequestParam("uname") String uname, @RequestParam("addr") String addr, @RequestParam("linkman") String linkman, @RequestParam("linktel") String linktel, @RequestParam("zhizhao") String zhizhao, @RequestParam("creator") String creator) {
        int r = rs.repairunitUpdate(uname, addr, linkman, linktel, zhizhao, creator, id);
        if (r == 0) {
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success("修改成功");
    }

    @RequestMapping(value = "delRepairunit", produces = "application/json;charset=utf-8")
    protected AjaxResult delRepairunit(@RequestParam("id") long id) {
        int r = rs.deleteById(id);
        if (r != 0) {
            return AjaxResult.success("解约成功");
        }
        return AjaxResult.error("解约失败");
    }

    @RequestMapping(value = "equipmentType", produces = "application/json;charset=utf-8")
    protected AjaxResult equipmentType() {
        List<EquipmentType> list = es.findType();
        return AjaxResult.success(list);
    }

    @RequestMapping(value = "equipmentList", produces = "application/json;charset=utf-8")
    protected Page<Equipment> equipmentList(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int pageSize, @RequestParam(name = "equipmenttypeid", defaultValue = "0") long equipmenttypeid, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "unitid", defaultValue = "0") long unitid) {
        return es.equipmentListBy(pageNo, pageSize, equipmenttypeid, name, unitid);
    }

    @RequestMapping(value = "equipmentGet", produces = "application/json;charset=utf-8")
    protected AjaxResult equipmentGet(@RequestParam("id") long id) {
        Equipment equipment = es.equipmentGet(id);
        if (equipment == null)
            return AjaxResult.error("设备不存在");
        return AjaxResult.success(equipment);
    }

    @RequestMapping(value = "equipmentUpdate", produces = "application/json;charset=utf-8")
    protected AjaxResult equipmentUpdate(Equipment equipment) {
        int r = es.equipmentUpdate(equipment);
        if (r == 0)
            return AjaxResult.error("修改失败");
        return AjaxResult.success("修改成功");
    }

    @RequestMapping(value = "changeStatus", produces = "application/json;charset=utf-8")
    protected AjaxResult changeStatus(@RequestParam("id") long id, @RequestParam("status") long status) {
        int r = 0;
        if (status == 1) {
            r = es.changeStatus(id, 0);
        } else {
            r = es.changeStatus(id, 1);
        }
        if (r == 0) {
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @RequestMapping(value = "baoXiuDanLimit", produces = "application/json;charset=utf-8")
    protected Page<BaoXiuDan> baoXiuDanLimit(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(value = "limit", defaultValue = "10") int pageSize, @RequestParam(name = "id", defaultValue = "0") long id) {
        return bxds.baoXiuDanLimit(pageNo, pageSize, id);
    }

    @RequestMapping(value = "baoxiumanList", produces = "application/json;charset=utf-8")
    protected Page<BaoXiuMan> baoxiumanList(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(value = "limit", defaultValue = "10") int pageSize, @RequestParam(name = "unitid", defaultValue = "0") long unitid, @RequestParam(name = "cname", required = false) String cname, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "openid", required = false) String openid, @RequestParam(name = "tel", required = false) String tel) {
        return bxms.baoxiumanList(unitid, pageNo, pageSize, cname, name, openid, tel);
    }

    @RequestMapping(value = "baoXiuManSearchById", produces = "application/json;charset=utf-8")
    protected AjaxResult baoXiuManSearchById(@RequestParam("id") long id) {
        BaoXiuMan man = bxms.baoXiuManSearchById(id);
        if (man == null) {
            return AjaxResult.error("用户不存在");
        }
        return AjaxResult.success(man);
    }

    @RequestMapping(value = "baoXiuManUpdate", produces = "application/json;charset=utf-8")
    protected AjaxResult baoXiuManUpdate(BaoXiuMan baoXiuMan) {
        int r = bxms.baoXiuManUpdate(baoXiuMan);
        if (r != 0) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    @RequestMapping(value = "baoXiuManUpdatePassById", produces = "application/json;charset=utf-8")
    protected AjaxResult baoXiuManUpdatePassById(@RequestParam("id") long id, @RequestParam("repass") String pass) {
        int r = bxms.baoXiuManUpdatePassById(id, pass);
        if (r == 0) {
            return AjaxResult.error("修改密码失败");
        }
        return AjaxResult.success("修改密码成功");
    }

    @RequestMapping(value = "delBaoXiuMan", produces = "application/json;charset=utf-8")
    protected AjaxResult delBaoXiuMan(@RequestParam("id") long id) {
        int r = bxms.delBaoXiuMan(id);
        if (r == 0) {
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }

    @RequestMapping(value = "unitAddBaoXiuMan", produces = "application/json;charset=utf-8")
    protected AjaxResult unitAddBaoXiuMan(BaoXiuMan baoXiuMan) {
        int r = bxms.unitAddBaoXiuMan(baoXiuMan);
        if (r == 0) {
            return AjaxResult.error("添加维保人员失败");
        }
        return AjaxResult.success("添加维保人员成功");
    }

    @RequestMapping(value = "unitAddEquipment", produces = "application/json;charset=utf-8")
    protected AjaxResult unitAddEquipment(Equipment equipment) {
        int r = es.unitAddEquipment(equipment);
        if (r == 0) {
            return AjaxResult.error("添加设备失败");
        }
        return AjaxResult.success("添加设备成功");
    }

    @RequestMapping(value = "unitBaoxiuman", produces = "application/json;charset=utf-8")
    protected AjaxResult unitBaoxiuman(@RequestParam("unitid") long unitid) {
        List<BaoXiuMan> list = bxms.findBaoXiuMan(unitid);
        if (list.size() == 0) {
            return AjaxResult.error("此单位没有维保人员");
        }
        return AjaxResult.success(list);
    }

    @RequestMapping(value = "delete", produces = "application/json;charset=utf-8")
    protected AjaxResult delete(@RequestParam("id") long id) {
        int r = es.delete(id);
        if (r == 0) {
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }

    @RequestMapping(value = "repairManPage", produces = "application/json;charset=utf-8")
    protected Page<RepairMan> repairManPage(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int pageSize, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "goodat", required = false) String goodat) {
        return rms.repairManPage(pageNo, pageSize, name, goodat);
    }

    //文件上传
    @RequestMapping(value = "upload", produces = "application/json;charset=utf-8")
    protected AjaxResult upload(MultipartFile file, HttpSession session, HttpServletRequest request) {
        String newName = "";
        if (!(file == null || file.isEmpty())) {
            String oldName = file.getOriginalFilename();
            String prefix=oldName.substring(oldName.lastIndexOf('.'));
             newName = UUID.randomUUID() + prefix;
            File path = new File(session.getServletContext().getRealPath("img"));
            if (path.exists() == false)
                path.mkdirs();
            try {
                file.transferTo(new File(path, newName));
            } catch (IOException e) {
                e.printStackTrace();
                return AjaxResult.error("上传失败");
            }
        }
        return AjaxResult.success(newName);
    }

    @RequestMapping(value = "findRepairMan", produces = "application/json;charset=utf-8")
    protected AjaxResult findRepairMan(@RequestParam("id") long id) {
        RepairMan rm = rs.findRepairMan(id);
        if (rm != null) {
            return AjaxResult.success(rm);
        }
        return AjaxResult.error("用户不存在");
    }

    @RequestMapping(value = "adminUpdateRepairMan", produces = "application/json;charset=utf-8")
    protected AjaxResult adminUpdateRepairMan(RepairMan repairMan) {
        int r = rs.adminUpdateRepairMan(repairMan);
        if (r == 0) {
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success("修改成功");
    }

    @RequestMapping(value = "delRepair", produces = "application/json;charset=utf-8")
    protected AjaxResult delRepair(@RequestParam("id") long id) {
        int r = rs.delRepair(id);
        if (r == 0) {
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success("删除成功");
    }

    @RequestMapping(value = "baoXiuDansOfRepairMan", produces = "application/json;charset=utf-8")
    protected Page<BaoXiuDan> baoXiuDansOfRepairMan(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int pageSize, @RequestParam(name = "id", defaultValue = "0") long id) {
        return bxds.baoXiuDansOfRepairMan(pageNo, pageSize, id);
    }

    @RequestMapping(value = "todoSearch", produces = "application/json;charset=utf-8")
    protected Page<Todo> todoSearch(@RequestParam(value = "page", defaultValue = "1") int pageNo, @RequestParam(value = "limit", defaultValue = "10") int pageSize, @RequestParam(name = "createdate",required = false) String createdate, @RequestParam(value = "status", defaultValue = "-1") int status, @RequestParam(value = "name", required = false) String name) {
        return ts.todoSearch(pageNo, pageSize, createdate, status, name);
    }
}
